package com.leal.myudf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 输入数据：hello,atguigu,hive
 * 输出数据:
 * hello
 * atguigu
 * hive
 */
public class MyUDTF extends GenericUDTF {


    private final ArrayList<String> outPutList = new ArrayList<>();

    /**
     *  一进多出
     * @param argOIs 输入的参数
     * @return 输出数据的集合
     * @throws UDFArgumentException 参数异常
     */
    @Override
    public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {

        //输出数据的默认列名,可以别别名覆盖
        List<String> fieldNames = new ArrayList<>();
        fieldNames.add("word");

        //输出数据的类型
        List<ObjectInspector> fieldOIs = new ArrayList<>();
        fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);

        //最终返回值
        return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
    }

    @Override
    public void process(Object[] args) throws HiveException {

        //2.按照"，"分割字符串
        String[] words = args[0].toString().split(",");
        //3.遍历数据写出
        for (String word : words) {
            //清空集合
            outPutList.clear();
            //将数据放入集合
            outPutList.add(word);
            //输出数据
            forward(outPutList);
        }

    }

    /**
     * 收尾方法
     * @throws HiveException ex
     */
    @Override
    public void close() throws HiveException {

    }
}
